//
//  DeviceHeadCellTableViewCell.m
//  carcareIOS
//
//  Created by wr on 16/4/7.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "DeviceHeadCell.h"

#define CORNER_Y (27.5 * __MAIN_RATIO_375)
#define CORNER_X (81 * __MAIN_RATIO_375)
#define CORNER_W (__MAIN_WIDTH - CORNER_X * 2)
#define CELL_Y (64 + SECTION_HEIGHT)
#define CELL_HEIGHT (CORNER_W + CORNER_Y)
#define PI 3.14159265358979323846

@interface DeviceHeadCell()

@property (nonatomic , strong) UILabel * deviceStatusLabel;

@property (nonatomic , strong) UIView * isLookReasonV;

@property (nonatomic , copy) void (^reasonClick)();

@end

@implementation DeviceHeadCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.deviceStatusLabel = [Factory labelWithFrame:CGRectMake(0, 0, CORNER_W - 70, CORNER_W - 70) font:FONT_DEFAULT_Bold(44) text:@"" textColor:COLOR_HEXSTRING(@"#FFFFFF") onView:self textAlignment:NSTextAlignmentCenter];
        [self.deviceStatusLabel setCenter:CGPointMake(__MAIN_WIDTH / 2 , (CELL_HEIGHT - CORNER_Y) / 2 + CELL_Y + 1 )];
        self.deviceStatusLabel.backgroundColor = [UIColor clearColor];
        self.deviceStatusLabel.numberOfLines = 2;
        
        self.isLookReasonV = [[[UIView viewWithFrame:CGRectMake(0, 0, self.deviceStatusLabel.width, 22)] resetBackgroundColor:[UIColor clearColor]] setupOnView:self];
        [self.isLookReasonV setCenter:CGPointMake(self.deviceStatusLabel.center.x, CORNER_W - 33 * __MAIN_RATIO_375 - self.isLookReasonV.height / 4 + CELL_Y)];
        
        UIImageView * reasonImageV = [Factory imageViewWithCenter:CGPointMake(0, 0) image:ImageNamed(@"lookReasonImage") onView:self.isLookReasonV];;
        
        __weak __typeof(self) wself = self;
        UILabel * reasonL = [Factory flabelWithCenter:CGPointMake(self.isLookReasonV.width / 2 - 8, self.isLookReasonV.height / 2) font:FONT_DEFAULT_Light(13) text:@"异常原因" textColor:[UIColor whiteColor] onView:self.isLookReasonV resetSizeFinish:^(UILabel *label) {
            [reasonL setSize:label.size];
            [reasonImageV setCenter:CGPointMake(wself.isLookReasonV.width / 2 - 5 + label.size.width / 2 + reasonImageV.width / 2, wself.isLookReasonV.height / 2 )];
        }];
        [[UIButton buttonEmptyWithFrame:self.isLookReasonV.bounds click:^{
            if(self.reasonClick)self.reasonClick();
        }] setupOnView:self.isLookReasonV];
        
    }
    return self;
}
- (void)setDeviceStatusStr:(NSString *)deviceStatusStr
{
    self.deviceStatusLabel.text = [NSString stringWithFormat:@"%@\n%@",[deviceStatusStr substringToIndex:2],[deviceStatusStr substringFromIndex:2]];
}
- (void)isLookReasonWithBool:(BOOL)isLookReason withBlock:(void(^)())clickBlock
{
    if(isLookReason) [self.deviceStatusLabel setCenterY:(CELL_HEIGHT - CORNER_Y) / 2 + 1 + CELL_Y  - 20];
    [self.isLookReasonV setHidden:!isLookReason];
    
    if(clickBlock && isLookReason) self.reasonClick = clickBlock;
}
+ (CGFloat)getCellHeight
{
    return CELL_HEIGHT + CELL_Y;
}
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 0xFF, 0xFF, 0xFF, 1);
    CGContextSetLineWidth(context, 1.0);
    CGContextAddArc(context, __MAIN_WIDTH / 2, (CELL_HEIGHT - CORNER_Y) / 2 + 1 + CELL_Y, CORNER_W / 2, 0, 2 * PI, 0);
    CGContextDrawPath(context, kCGPathStroke); //绘制路径
    CGContextStrokePath(context);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
